package org.csu.mall.controller.front;

import org.csu.mall.common.CONSTANT;
import org.csu.mall.common.UniformResponse;
import org.csu.mall.entity.Account;
import org.csu.mall.entity.CommodityRate;
import org.csu.mall.entity.CustomerOrder;
import org.csu.mall.service.IAccountService;
import org.csu.mall.service.ICommodityRateService;
import org.csu.mall.util.PropertiesUtil;
import org.csu.mall.vo.AccountListVO;
import org.csu.mall.vo.AccountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin/")
public class AccountController {

    @Autowired
    public IAccountService accountService;

    @Autowired
    ICommodityRateService commodityRateService;

    @PostMapping("login")
    @ResponseBody
    public UniformResponse<Account> login(String username, String password, HttpSession session){
        UniformResponse response = accountService.login(username, password);
        if(response.isSuccess()){
            session.setAttribute(CONSTANT.LOGIN_USER, response.getData());
        }
        session.setAttribute(CONSTANT.IMAGE_HOST, PropertiesUtil.getProperty("image.server.url"));
        return response;
    }

    @PostMapping("register")
    @ResponseBody
    public UniformResponse<String> register(Account account){
        return accountService.register(account);
    }

    @PostMapping("check_field")
    @ResponseBody
    public UniformResponse<String> checkFiled(String type, String value){
        return accountService.checkField(type, value);
    }

    @PostMapping("forget_password")
    @ResponseBody
    public UniformResponse<String> forgetPassword(String type, String value){
        return accountService.forgetPassword(type, value);
    }

    @PostMapping("check_answer")
    @ResponseBody
    public UniformResponse<String> checkAnswer(String username, String question, String answer){
        return accountService.checkAnswer(username, question, answer);
    }

    @PostMapping("forget_reset")
    @ResponseBody
    public UniformResponse<String> forgetReset(String username, String forgetToken, String newPassword){
        return accountService.forgetReset(username, forgetToken, newPassword);
    }

    @PostMapping("reset_password")
    @ResponseBody
    public UniformResponse<String> resetPassword(
            String oldPassword, String newPassword, String answer, HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        return accountService.resetPassword(account, oldPassword, newPassword, answer);
    }

    @PostMapping("update_account")
    @ResponseBody
    public UniformResponse<Account> modifyAccountInfo(Account account, HttpSession session){
        Account oldAccount = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        UniformResponse response = accountService.modifyAccountInfo(account, oldAccount);
        if(response.isSuccess()){
            session.setAttribute(CONSTANT.LOGIN_USER, response.getData());
        }
        return response;
    }

    @GetMapping("get_account")
    @ResponseBody
    public UniformResponse<AccountVO> getAccount(HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        return accountService.getAccount(account);
    }

    @PostMapping("authentication")
    @ResponseBody
    public UniformResponse<String> authentication(
            String realName, Long idCard, String answer, HttpSession session){
        //todo 增加密码验证
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        return accountService.authentication(account, realName, idCard, answer);
    }

    @PostMapping("recharge_account")
    @ResponseBody
    public UniformResponse<Account> rechargeAccount(Double balance, HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        UniformResponse response = accountService.rechargeAccount(account, balance);
        if(response.isSuccess()){
            session.setAttribute(CONSTANT.LOGIN_USER, response.getData());
        }
        return response;
    }

    @PostMapping("add_address")
    @ResponseBody
    public UniformResponse<Account> addAddress(String address, HttpSession session) {
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        UniformResponse response = accountService.addAddress(account, address);
        if(response.isSuccess()){
            session.setAttribute(CONSTANT.LOGIN_USER, response.getData());
        }
        return response;
    }

    @PostMapping("logout")
    @ResponseBody
    public UniformResponse<String> logout(HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        UniformResponse response = accountService.logout(account);
        if(response.isSuccess()){
            session.removeAttribute(CONSTANT.LOGIN_USER);
        }
        return response;
    }

    //------------------------------------商品评价---------------------------------
    @PostMapping("review")
    @ResponseBody
    public UniformResponse<String> reviewCommodity(CustomerOrder customerOrder, HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        //测试
//        Account account1 = new Account();
//        account1.setUserId(1001);
        return commodityRateService.reviewCommodity(customerOrder, account);
    }


    //--------------------------后台--------------------------
    @GetMapping("get_account_list")
    @ResponseBody
    public UniformResponse<AccountListVO> getAccountList(
            @RequestParam(name = "role", defaultValue = "4") Integer role,
            @RequestParam(name = "username", required = false) String username,
            @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
            @RequestParam(name = "pageNum", defaultValue = "1") int pageNum){
        return accountService.getAccountList(role, username, pageSize, pageNum);
    }

    //新增
    @PostMapping("update_account_image")
    @ResponseBody
    public UniformResponse<Account> updateAccountImage(String imgAddress, HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        UniformResponse response = accountService.updateAccountImage(imgAddress, account);
        if(response.isSuccess()){
            session.setAttribute(CONSTANT.LOGIN_USER, response.getData());
        }
        return response;
    }

}
